<?php

$FamilyId = $_GET['FamilyId'];
$RedirectLocation = @$_GET['RedirectLocation'];
$RedirectArgs = @$_GET['RedirectArgs'];
$PHPSESSID = @$_GET['PHPSESSID'];

echo '<div id="page_header">'."\n";
echo '<ul><li class="titlename2">Deleting Family...</li>'."\n";
echo '</ul>'."\n";
echo '</div>'."\n";
echo '<div id="main_content">'."\n";

if($PACTS_cfg->getUserCanDelete()) {
    if(strlen($FamilyId)>0) {
        //        $Dusername = $PACTS_cfg->getLookValue("Individual", "IndividualId", $IndividualId, "Surname");

        mysql_query("DELETE FROM Family WHERE FamilyId=".$FamilyId,$PACTS_cfg->dblink);
        //	echo "Record deleted!<br />DELETE FROM Family WHERE FamilyId=$FamilyId<br />";
        $count =0;

        if(mysql_affected_rows()>0 or TRUE) {
            $PACTS_cfg->InsertAuditTrailEntry( "", "" ,"Delete","Family","COMPLETE ENTRY",$_SESSION['username'], $_SERVER['PHP_SELF'],$FamilyId."");

            $URLmessage="<p>Family has been DELETED sucessfully!";
            $result = mysql_query("Select IndividualId FROM Individual WHERE FamilyId=".$FamilyId,$PACTS_cfg->dblink);
            while ($line = mysql_fetch_assoc($result)) {
                foreach($line as $key=>$val) {
                    //echo "$key :: $val<p>";
                    //echo "DELETE FROM Individual WHERE IndividualId=$val<br />";
                    $count = $count+ 1;
                    mysql_query("DELETE FROM ".$PACTS_cfg->individualtablename." WHERE IndividualId=".$val,$PACTS_cfg->dblink);
                }

            }
            //echo "deleted individuals : $count <br>";
            if($count>0) {
                $URLmessage.="<br />".$count." Indiviudal";
                if($count!=1) {$URLmessage.="s from that family have";}
                if($count==1) {$URLmessage.=" from that family has";}
                $URLmessage=$URLmessage." been DELETED sucessfully!";

            }
            //            echo "$URLmessage";
        } else {

            $URLmessage="ERROR: Could not DELETE family! : ".mysql_error();

        }

        if(strlen($RedirectLocation)==0) {
            $RedirectLocation = $DefaultRedirectLocation;
        }

        $URL = "Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$RedirectLocation.".php?";	$URL = $URL."FamilyId=$FamilyId&PHPSESSID=".$PHPSESSID."&URLmessage=$URLmessage";
        if(strlen($RedirectArgs)>0) {
            $URL = $URL."&$RedirectArgs";
        }
        echo $URLmessage."</p>\n";
/*
            echo '<li class="type2">Click ';
            echo '<a href="index.php?view=Fmly_view&amp;'.'FamilyId='.$FamilyId.'&amp;IndividualId='.$IndividualId.'">here</a>';
            echo ' to view the Family again</li>'."\n";
 *
 */


    } else {
        echo "Error: Could not delete record!<br /><br />";
    }

}
else {
    echo '<div id="details-section">'."\n";
    echo '<div id="viewDetailsC2">'."\n";

    echo '<p class="errmsg1">Error! You do not have permission to delete data!</p>'."\n";
    echo '</div>'."\n";
    echo '</div>'."\n";
} 
echo '</div>'."\n";

?>
